package com.yq.ylmall.config;

import com.yq.ylmall.interceptors.UserInfoInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConditionalOnClass(DispatcherServlet.class)
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInfoInterceptor())
                .excludePathPatterns(
                        "/admin/system/index/generateValidateCode",
                        "/admin/system/index/login",
                        "/swagger**/**",
                        "/webjars/**",
                        "/v3/**",
                        "/doc.html",
                        "/admin/product/category/import-data",
                        "/admin/product/region/importData",
                        "/admin/payment/alipay/**",
                        "/api/payment/wechat/native/notify",
                        "/api/payment/wechat/refund/notify",
                        "/api/order/update/**",
                        "/api/order/getOrderInfoByOrderNo/**",
                        "/api/**");
    }


    //springboot2.x 静态资源在自定义拦截器之后无法访问的解决方案
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }

}
